<template>
  <div>
    <ActionSheet v-model="show" :closeable="false">
      <div class="ActionSheet">
        <span class="res_icon flex justify-between items-center" @click="reset">
          <img
            :src="require('@/assets/workbench/goout/reset.png')"
            width="16px"
            height="16px"
            alt=""
          />
          重新筛选</span
        >
        <div v-for="(item, index) in list" :key="index" class="ll_filter">
          <div class="mb-20px" v-show="item.type === 1">
            <div class="lable mb-14px">{{ item.lable }}（单选）</div>
            <div class="flex flex-wrap">
              <div
                v-for="(val, d) in item.value"
                :key="d"
                class="ll_botton mb-12px mr-8px"
                @click="handclick(val, index)"
                :class="
                  act_data[index] && act_data[index].data.type === val.type
                    ? 'active'
                    : null
                "
              >
                <span> {{ val.name }}</span>
              </div>
            </div>
          </div>
          <!-- 时间 -->
          <div class="flex flex-wrap mb-20px" v-if="item.type === 2">
            <div class="lable mb-14px">{{ item.lable }}</div>
            <div
              class="flex datefilter justify-between mb-12px"
              @click="opentimeFilter"
            >
              <span
                :class="act_data[index] && act_data[index].data ? '' : 'plac'"
                >{{
                  act_data[index] && act_data[index].data
                    ? act_data[index].data[0] + " 至 " + act_data[index].data[1]
                    : "请选择"
                }}</span
              >
              <img
                :src="require('@/assets/icon/date.png')"
                style="width: 14px; height: 14px"
              />
            </div>
          </div>
          <!-- 品类 -->
          <div class="flex flex-wrap mb-24px" v-if="item.type === 3">
            <div class="lable mb-14px">{{ item.lable }}</div>
            <div style="width: 100%">
              <Field
                readonly
                clickable
                :value="act_data[index] && act_data[index].data.lable"
                placeholder="请选择"
                @click="openSele"
                class="selection pl-14px pr-17px"
                size="small"
              >
                <template #right-icon>
                  <img
                    :src="require('@/assets/icon/arrow-right.png')"
                    width="14px"
                    height="14px" /></template
              ></Field>
            </div>
          </div>
        </div>
        <popupAction @cancel="close" @confirm="confirm"></popupAction>
      </div>
    </ActionSheet>
    <timeFilter ref="timeFilter" @submitTime="submitTime" @closeDate="open">
    </timeFilter>
    <selections
      ref="selections"
      @closeSlection="closeSlection"
      @confirmSel="confirmSel"
    ></selections>
  </div>
</template>

<script>
import { ActionSheet, Field, Toast } from "vant";
import { data_status, category } from "@/services/business/dict.js";
import popupAction from "./popup_action.vue";
import timeFilter from "@/components/ll_time_filter";
import selections from "./selections.vue";
export default {
  components: {
    ActionSheet,
    Field,
    popupAction,
    timeFilter,
    selections,
  },
  data() {
    return {
      show: false,
      list: [
        { lable: "数据状态", value: [], type: 1 },
        { lable: "跟进状态", value: [], type: 1 },
        { lable: "商机状态", value: [], type: 1 },
        { lable: "商机类型", value: [], type: 1 },
        { lable: "进入时间", value: "", type: 2 },
        { lable: "品类", value: [], type: 3 },
      ],
      value1: "",
      categoryval: "", //品类
      typelist: {
        0: "business_opportunity_data_status", //商机数据状态
        1: "follow_up_status", //商机跟进状态
        2: "business_opportunity_status", //商机状态
        3: "business_opportunity_type", // 商机类型
        // 5: "business_opportunity_input_type", //商机录入类型
      },
      act_data: [],
      fieldNames: {
        text: "name",
        value: "id",
        children: "children",
      },
      activetime: {}, //时间选择数据
      selectionsData: {}, //品类选择数据
    };
  },
  mounted() {
    this.get_data(0);
    this.get_data(1);
    this.get_data(2);
    this.get_data(3);
    this.get_category(5);
    this.act_data[4] = {
      index: 4,
      data: this.list[4].value,
      type: this.list[4].type,
    };
  },
  methods: {
    get_data(type) {
      // 商机数据状态
      data_status(this.typelist[type])
        .then((res) => {
          this.list[type].value = res;
          this.act_data[type] = {
            index: type,
            data: this.list[type].value[0],
            type: this.list[type].type,
          };
        })
        .catch((err) => {
          Toast(err.errMessage || err.message);
        });
    },
    get_category(type) {
      category()
        .then((res) => {
          this.list[type].value = res.children;
          this.act_data[type] = {
            index: type,
            data: "",
            type: this.list[type].type,
          };
        })
        .catch((err) => {
          Toast(err.errMessage || err.message);
        });
    },
    open() {
      this.show = true;
    },
    handclick(value, index) {
      this.act_data.splice(index, 1, {
        index: index,
        data: value,
        type: this.list[index].type,
      });
    },
    // 打开品类选择
    openSele() {
      this.close();
      this.$refs.selections.open(this.selectionsData);
    },
    close() {
      this.show = false;
    },
    confirm() {
      let arr = JSON.parse(JSON.stringify(this.act_data));
      let data = arr.filter((item, index) => {
        item.key = index;
        item.lable = this.list[index].lable;
        if (item.type === 1) {
          item.name = item.data.name;
          return item.data.type != this.list[index].value[0].type;
        } else if (item.type === 2 && item.data.length > 0) {
          item.name = item.data[0] + "至" + item.data[1];
        } else if (item.type === 3) {
          item.name = item.data.lable;
        }
        return item.data;
      });
      this.$emit("searchConfirm", data);
      this.close();
    },
    clear() {
      this.list.forEach((item, index) => {
        if (item.type === 1) {
          this.act_data.splice(index, 1, {
            index: index,
            data: this.list[index].value[0],
            type: this.list[index].type,
          });
        } else {
          this.act_data.splice(index, 1, {
            index: index,
            data: "",
            type: this.list[index].type,
          });
          item.value = "";
        }
      });
    },
    // 点击重置
    reset() {
      this.clear();
      this.$emit("reset");
    },
    // 打开时间选择面板
    opentimeFilter() {
      let val;
      if (this.activetime) {
        val = this.activetime;
      }
      this.$refs.timeFilter.open(val);
    },
    // 选择时间
    submitTime(val) {
      if (val) {
        this.list[4].value = val.showVisitTime;
        this.act_data[4] = { index: 4, data: val.showVisitTime, type: 2 };
        this.activetime = val;
      }
    },
    // 关闭品类选择
    closeSlection() {
      this.show = true;
    },
    // 提交品类选择
    confirmSel(data) {
      let r = [];
      data.value.forEach((item) => {
        r.push(item.name);
      });
      this.selectionsData = data;
      this.act_data[5] = {
        index: 5,
        data: { value: data.value, lable: r.join("-") },
        type: 3,
      };
      this.show = true;
    },
    closeTabs(row) {
      if (row.data.name) {
        this.act_data[row.index].data = this.list[row.index].value[0];
      } else {
        this.act_data[row.index].data = "";
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.ActionSheet {
  padding: 20px 18px 0 16px;
  padding-bottom: calc(50px + env(safe-area-inset-bottom));
  position: relative;
  .res_icon {
    font-size: 12px;
    font-family: SFPro-RegularItalic, SFPro;
    font-weight: normal;
    color: #1d6fe9;
    position: absolute;
    top: 20px;
    right: 18px;
    // width: 66px;
  }
  .ll_filter {
    font-family: PingFangSC-Medium, PingFang SC;
    .lable {
      height: 20px;
      font-size: 14px;
      font-weight: 500;
      color: #333333;
      line-height: 20px;
    }
    .ll_botton {
      height: 32px;
      padding: 0 26px;
      background: #f5f5f5;
      border-radius: 16px;
      font-size: 12px;
      display: flex;
      justify-content: center;
      align-content: center;
      box-sizing: border-box;
      line-height: 32px;
      border: 1px solid #f5f5f5;
    }
  }
  .active {
    background-color: rgba(29, 111, 233, 0.1) !important;
    border-radius: 16px;
    border: 1px solid #1d6fe9 !important;
    color: #1d6fe9;
  }
  .datefilter {
    width: 339px;
    height: 32px;
    background: #f5f5f5;
    border-radius: 16px;
    padding: 8px 9px 8px 14px;
    box-sizing: border-box;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    .img {
      width: 14px;
      height: 14px;
      max-width: none;
    }
    .plac {
      color: #999999;
    }
  }
  .selection {
    width: 100%;
    height: 32px;
    background: #f5f5f5;
    border-radius: 16px;
    ::v-deep .van-cell__value {
      height: 100%;
      .van-field__body {
        height: 100%;
        font-family: PingFangSC-Regular, PingFang SC;
        .van-field__control {
          color: #333333;
          font-size: 12px;
          font-family: PingFangSC-Regular, PingFang SC;
        }
      }
    }
  }
  ::v-deep.van-field__control::placeholder {
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
  }
}
</style>
